package com.t.bricks.bussiness.db.dao.recharge.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.recharge.RechargeDao;
import com.t.bricks.bussiness.db.entity.recharge.Recharge;
import com.t.bricks.bussiness.db.entity.recharge.base.CustomerAccountBase;
import com.t.bricks.bussiness.db.entity.recharge.base.TallyBase;
import com.t.bricks.bussiness.db.entity.recharge.base.WechatRefundBase;
import com.t.bricks.bussiness.db.mapper.recharge.RechargeMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.Page;

@Repository
public class RechargeDaoImpl implements RechargeDao {

	@Autowired
	private RechargeMapper rechargeMapper;
	
	/**
	 * 添加
	 * @Param recharge
	 * @return int
	 */
	@Override
	public MsgEmity add(Recharge recharge) {
		try {
			int i = rechargeMapper.add(recharge);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", recharge);
	}

	/**
	 * 标记删除
	 * @Param recharge
	 * @return int
	 */
	@Override
	public MsgEmity signDel(Recharge recharge) {
		try {
			int i = rechargeMapper.signDel(recharge);
			if (i < 1) {
				Map<String, Object> map = rechargeMapper.getCheckField(recharge.getuId(), null);//如果影响数小于1,则检查数据
				if (null == map) {
					return new MsgEmity(false, "没有找到对应数据！", 7001);//删除影响数失败,但数据也不存在则视为成功
				}
				
				if (recharge.getiVersion() != Integer.valueOf(map.get("iVersion").toString()).intValue()) {
					return new MsgEmity(false, "更新状态失败，系统中的数据可能已经被更新！", 7002);
				}
				
				if (Integer.valueOf(map.get("iState").toString()) < 1) {
					return new MsgEmity(false, "更新状态失败，系统中的数据已经删除！", 7003);
				}
				
				if (null == recharge.getsRecordKey() || recharge.getsRecordKey().equals(map.get("sRecordKey"))) {
					return new MsgEmity(false, "更新状态失败，记录验证串不符合！", 7004);
				}

				Log.error("更新状态未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "更新状态数据失败！", 7005);
			}
		} catch (Exception e) {
			Log.error("更新状态时发生异常:", e.getMessage());
			return new MsgEmity(false, "更新状态失败！", 7006);
		}
		
		return new MsgEmity(true, "更新状态成功！", 7999);
	}

	/**
	 * 修改状态
	 * @param recharge
	 * @param customerAccountBase
	 * @return
	 */
	@Override
	public MsgEmity changeState(Recharge recharge) {
		if (recharge.getiState() == 4) {
			return new MsgEmity(false, "不能在此更新待同步状态！", 7001);
		}
		
		try {
			int i = rechargeMapper.changeState(recharge.getiState(),
					recharge.getsOrderNo(),
					recharge.getdModifiedDate(),
					recharge.getiVersion(),
					recharge.getsRecordKey());
			if (i < 1) {
				Map<String, Object> map = rechargeMapper.getCheckField(null, recharge.getsOrderNo());//如果影响数小于1,则检查数据
				if (null == map) {
					return new MsgEmity(false, "没有找到对应数据！", 7002);//删除影响数失败,但数据也不存在则视为成功
				}
				
				if (recharge.getiVersion() != Integer.valueOf(map.get("iVersion").toString()).intValue()) {
					return new MsgEmity(false, "更新状态失败，系统中的数据可能已经被更新！", 7003);
				}
				
				if (Integer.valueOf(map.get("iState").toString()) < 1) {
					return new MsgEmity(false, "更新状态失败，系统中的数据已经删除！", 7004);
				}
				
				if (null == recharge.getsRecordKey() || recharge.getsRecordKey().equals(map.get("sRecordKey"))) {
					return new MsgEmity(false, "更新状态失败，记录验证串不符合！", 7005);
				}

				Log.error("更新状态未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "更新状态数据失败！", 7006);
			}
		} catch (Exception e) {
			Log.error("更新状态时发生异常:", e.getMessage());
			return new MsgEmity(false, "更新状态失败！", 7006);
		}
		
		return new MsgEmity(true, "更新状态成功！", 7999);
	}

	/**
	 * 根据编号查询指定订单
	 * @param uId
	 * @return
	 */
	@Override
	public MsgEmity findById(String uId) {
		Recharge recharge = null;
		try {
			recharge = rechargeMapper.findById(uId);
			if (null == recharge) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", recharge);
	}

	/**
	 * 查询指定订单
	 * @param sOrderNo
	 * @return
	 */
	@Override
	public MsgEmity findByOrder(String sOrderNo) {
		Recharge recharge = null;
		try {
			recharge = rechargeMapper.findByOrder(sOrderNo);
			if (null == recharge) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", recharge);
	}

	/**
	 * 查询指定订单的状态
	 * @param sOrderNo
	 * @return
	 */
	@Override
	public MsgEmity findState(String sOrderNo) {
		Integer iState = null;
		try {
			iState = rechargeMapper.findState(sOrderNo);
			if (null == iState) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", iState);
	}

	/**
	 * 查询指定状态的订单
	 * @param iState
	 * @return
	 */
	@Override
	public MsgEmity findByState(int iState) {
		List<Recharge> list = null;
		try {
			list = rechargeMapper.findByState(iState);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

	/**
	 * 查询指定订单是否存在(存在返回值大于0)
	 * @param sOrderNo
	 * @return
	 */
	@Override
	public MsgEmity findCountByOrder(String sOrderNo) {
		int iCount = 0;
		try {
			iCount = rechargeMapper.findCountByOrder(sOrderNo);
			if (0 == iCount) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", iCount);
	}
	
	/**
	 * 查询分页信息
	 * @param iStates
	 * @param page
	 * @return
	 */
	@Override
	public MsgEmity findByPage(Integer[] iStates, Page page) {
		int iCount = 0;
		try {
			iCount = rechargeMapper.findByPageCount(iStates);
		} catch (Exception e) {
			Log.error("查询数量发生异常", e.getMessage());
			return new MsgEmity(false, "查询数量失败！", 7001);
		}
		
		if (iCount < 1) {
			return new MsgEmity(true, "没有相关数据！", page);
		}

		page.setCountRow(iCount);

		List<Recharge> list = null;
		try {
			list = rechargeMapper.findByPage(iStates, page);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}

		page.setRecordList(list);
		
		return new MsgEmity(true, "查询成功！", page);
	}
	
	/**
	 * 取充值对应的积分
	 * @param iType
	 * @param sValue
	 * @return
	 */
	@Override
	public MsgEmity getRechargeTally(int iType, String sValue) {
		Integer iTally = null;
		try {
			iTally = rechargeMapper.getRechargeTally(iType, sValue);
			if (null == iTally) {
				return new MsgEmity(true, "数据不存在,判定为0！", 0);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", iTally);
	}
	
	/**
	 * 查询顾客账户
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity findCustomer(String sId) {
		CustomerAccountBase customerAccountBase = null;
		try {
			customerAccountBase = rechargeMapper.findCustomer(sId);
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", customerAccountBase);
	}
	
	/**
	 * 同步更新顾客账户中的余额与积分
	 * @param recharge
	 * @param tallyBase
	 * @param customerAccountBase
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity changeCustomerAccount(Recharge recharge, TallyBase tallyBase,
			CustomerAccountBase customerAccountBase) {
		if (recharge.getiState() != 4) {
			return new MsgEmity(false, "此方法只能用于修改状态为4的订单！", 7001);//删除影响数失败,但数据也不存在则视为成功
		}
		
		try {
			int i = rechargeMapper.changeState(recharge.getiState(),
					recharge.getsOrderNo(),
					recharge.getdModifiedDate(),
					recharge.getiVersion(),
					recharge.getsRecordKey());
			if (i < 1) {
				return new MsgEmity(false, "更新状态数据失败！", 7006);
			}
			
			i = rechargeMapper.addTally(tallyBase);
			if (i < 1) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "更新状态数据失败,未能添加积分日志信息！", 7007);
			}
			
			i = rechargeMapper.changeCustomerAccount(customerAccountBase);
			if (i < 1) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "更新状态数据失败,未能更新账户信息！", 7008);
			}
		} catch (Exception e) {
			Log.error("更新状态时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "更新状态失败！", 7009);
		}
		
		return new MsgEmity(true, "更新状态成功！", 7999);
	}
	
	/**
	 * 错误值累加
	 * @param uId
	 * @return
	 */
	@Override
	public MsgEmity addErrorCount(String uId) {
		try {
			int i = rechargeMapper.addErrorCount(uId);
			if (i < 1) {
				return new MsgEmity(false, "错误值累加失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("错误值累加发生异常:", e.getMessage());
			return new MsgEmity(false, "错误值累加失败！", 7002);
		}
		
		return new MsgEmity(true, "错误值累加成功！", 7999);
	}

	/**
	 * 新增微信退款记录
	 * @param wechatRefund
	 * @return
	 */
	@Override
	public MsgEmity addWechatRefund(WechatRefundBase wechatRefund) {
		try {
			int i = rechargeMapper.addWechatRefund(wechatRefund);
			if (i < 1) {
				return new MsgEmity(false, "新增失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增发生异常:", e.getMessage());
			return new MsgEmity(false, "新增发生异常！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", 7999);
	}
	
	/**
	 * 判断微信退款记录是否存在,存在返回非0
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity hasWechatRefund(String sId) {
		try {
			int i = rechargeMapper.hasWechatRefund(sId);
			return MsgEmity.success(i, "退款记录", (i == 0 ? "不存在" : "存在"));
		} catch (Exception e) {
			Log.error("'判断微信退款记录是否存在'发生异常:", e.getMessage());
			return new MsgEmity(false, "'判断微信退款记录是否存在'发生异常", 7001);
		}
	}

}
